<?php
/**
 * CK框架 目录类
 * @author wujibing<283109896@qq.com>
 * @version   3.0
 * @category   CkFrameWork
 * @package  CkFrameWork
 * @subpackage lib
 */
class dir
{
    /**
     * 建立一个目录
     * @param string $dirPath 目录路径
     * @param int $mode 目录权限ֵ
     */
    public static function mkDir($dirPath,$mode = 0777)
    {
        if (!is_dir($dirPath))
        {
            $temp = explode('/', $dirPath);
            $curDir = '';
            $num = count($temp);
            for ($i = 0; $i < $num; $i ++)
            {
            $curDir .= $temp[$i] . '/';
            if (!is_dir($curDir))
                mkdir($curDir, $mode);
            }
        }
    }
    /**
     * 扫描一个文件夹并获得该文件夹下的文件和文件夹数组
     * @param string $dirPath 文件夹目录
     * @throws isNotADirException 传入的文件夹不是文件夹
     * @return array 返回数组
     * array('fileName' => '文件名' ,'isDir' => 'bool是否是文件夹','filePath'=>'文件全路径')
     */
    public static function scanDir($dirPath)
    {
        $tmpArray = array();
        if(is_file($dirPath) || is_link($dirPath) || !is_dir($dirPath))
        {
            throw new isNotADirException($dirPath);
        }
        $fileList = scandir($dirPath);
        $tmpFilePath = '';
        if($fileList) foreach ($fileList as $tmpFile)
        {
            if($tmpFile == '.' || $tmpFile == '..')
            {
                continue;
            }
            $tmpFilePath = $dirPath.$tmpFile;
            $tmpArray[] = array('fileName' => $tmpFile ,'isDir' => is_dir($tmpFilePath) ,'filePath' =>$tmpFilePath);
        }
        return $tmpArray;
    }
    /**
     * 删除一个目录
     * @param unknown_type $dirPath
     */
    public static function delDir($dirPath)
    {
        if(is_file($dirPath || is_link($dirPath)))
        {
            return @unlink($dirPath) or self::throwDelException($dirPath);
        }
        if($dirPath[strlen($dirPath) -1] != '/')
        {
            $dirPath .= '/';
        }
        if(is_dir($dirPath))
        {
            //scan dir
            $fileList = scandir($dirPath);
            if(!$fileList)
            {
                return @rmdir($dirPath) or self::throwDelException($dirPath);
            }
            foreach ($fileList as $file)
            {
                if($file == '.' || $file == '..')
                {
                    continue;
                }
                if(is_dir($file))
                {
                    self::delDir($dirPath.$file);
                } else
                {
                    @unlink($dirPath.$file) or self::throwDelException($dirPath);
                }
            }
            @rmdir($dirPath) or self::throwDelException($dirPath);
        }
    }
    /**
     * 抛出不能删除的异常
     * @param string $dirPath
     * @throws delDirException
     */
    private static function throwDelException($dirPath)
    {
        throw new delDirException($dirPath);
    }
}


/**
 * 不是一个文件夹异常
 * @author wujibing<283109896@qq.com>
 * @version   3.0
 * @category   CkFrameWork
 * @package  CkFrameWork
 * @subpackage lib
 */
class isNotADirException extends Exception
{
    public function __construct($dirName)
    {
        parent::__construct($dirName);
    }
}

/**
 * 删除文件夹失败异常
 * @author wujibing<283109896@qq.com>
 * @version   3.0
 * @category   CkFrameWork
 * @package  CkFrameWork
 * @subpackage lib
 */
class delDirException extends Exception
{
    public function __construct($dirName)
    {
        parent::__construct($dirName);
    }
}